package com.example.business_oa.controller;

import com.example.business_oa.service.SysEmployeeService;
import com.example.business_oa.service.SysUserService;
import com.example.comment_oa.pojos.SysEmployee;
import com.example.comment_oa.pojos.SysUser;
import com.example.comment_oa.response.AjaxResponse;
import com.example.comment_oa.util.JwtUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@CrossOrigin
@RequestMapping("/businessOa")
public class LoginContoller {
    @Resource
    SysEmployeeService sysEmployeeService;
    @Resource
    SysUserService sysUserService;

    @PostMapping("/login/{username}/{userPassword}")
    public AjaxResponse login(@PathVariable("username") String username,
                              @PathVariable("userPassword") String userPassword){
        SysUser sysUser = sysUserService.login(username, userPassword);
        String jwt = JwtUtil.createJWT(username, "back", "user", 1000 * 60 * 60 *2);
        Set<String> userPermission = sysUserService.findUserPermissionByid(sysUser.getUserId());
        SysEmployee sysEmployee = sysEmployeeService.selectEmpId(sysUser.getUserId());
        sysUser.setSysEmployee(sysEmployee);
        sysUser.setUserPassword(null);
        Map<String,Object> map=new HashMap<>();
        map.put("permission",userPermission);
        map.put("token",jwt);
        map.put("user",sysUser);
        return AjaxResponse.success(map);
    }

}
